#include "gpio.h"
#include <QDebug>
#include <QFile>


Gpio::Gpio()
{
    qDebug() << "gpio gouzao func" << endl;
    GpioConfig(115,"in");
    GpioConfig(107,"in");
    GpioConfig(108,"in");
}

Gpio::~Gpio()
{

    qDebug() << "gpio xigou func" << endl;
    GpioDeconfig(115);
    GpioDeconfig(107);
    GpioDeconfig(108);

}

void Gpio::GpioConfig(uint8_t pin, QString direction)
{
    QString configPinPath = QString("/sys/class/gpio/export");
    QFile configPinFile(configPinPath);
    if(!configPinFile.open(QIODevice::WriteOnly))
    {
        qDebug() << "GpioConfig pin open gpio file fail" << endl;
        return;
    }
    QString pinConfig = QString("%1").arg(pin);
    configPinFile.write(pinConfig.toUtf8());
    configPinFile.close();

    QString configDirPath = QString("/sys/class/gpio/gpio%1/direction").arg(pin);
    QFile configDirFile(configDirPath);
    if(!configDirFile.open(QIODevice::ReadWrite))
    {
        qDebug() << "GpioConfig dir open gpio file fail" << endl;
        return;
    }
    configDirFile.write(direction.toUtf8());
    configDirFile.close();
}

void Gpio::GpioDeconfig(uint8_t pin)
{
    QString deconfigPath = QString("/sys/class/gpio/unexport");
    QFile deconfigFile(deconfigPath);
    if(!deconfigFile.open(QIODevice::WriteOnly))
    {
        qDebug() << "GpioDeconfig open gpio file fail" << endl;
        return;
    }
    QString unexportConfig = QString("%1").arg(pin);
    deconfigFile.write(unexportConfig.toUtf8());
    deconfigFile.close();
}

uint8_t Gpio::GpioReadVal(uint8_t pin)
{
    QString valueFilePath = QString("/sys/class/gpio/gpio%1/value").arg(pin);
    QFile valueFile(valueFilePath);

    if(!valueFile.open(QIODevice::ReadOnly))
    {
        qDebug() << "GpioReadVal open gpio file fail" << endl;
        return 2;
    }
    QByteArray valueData = valueFile.readAll();
    valueFile.close();

    if(!valueData.isEmpty())
    {
        uint8_t value = valueData.trimmed().toUInt();
        return value;
    }
    return 2;
}

void Gpio::GpioWriteVal(uint8_t pin,uint8_t value)
{
    QString configValPath = QString("/sys/class/gpio/gpio%1/value").arg(pin);
    QFile configValFile(configValPath);
    if(!configValFile.open(QIODevice::ReadWrite))
    {
        qDebug() << "GpioWriteVal open gpio file fail" << endl;
        return;
    }
    QString valConfig = QString("%1").arg(value);
    configValFile.write(valConfig.toUtf8());
    configValFile.close();
}

QList<uint8_t> Gpio::KeyValGet(const QList<uint8_t>& pins)
{
    QList<uint8_t> pressedKeys;

    for (uint8_t pin : pins) {
        QString valueFilePath = QString("/sys/class/gpio/gpio%1/value").arg(pin);
        QFile valueFile(valueFilePath);

        if (!valueFile.exists()) {
            qDebug() << "GPIO value file does not exist:" << valueFilePath;
            continue;
        }

        if (!valueFile.open(QIODevice::ReadOnly)) {
            qDebug() << "Failed to open GPIO value file:" << valueFilePath;
            continue;
        }

        QByteArray valueData = valueFile.readAll();
        valueFile.close();

        if (valueData.isEmpty()) {
            qDebug() << "GPIO value file is empty:" << valueFilePath;
            continue;
        }

        bool ok;
        uint8_t value = valueData.trimmed().toInt(&ok);
        if (!ok) {
            qDebug() << "Failed to parse GPIO value from:" << valueFilePath;
            continue;
        }

        if (value == 0) {
            pressedKeys.append(pin);  // 检测到按键按下
        }
    }

    return pressedKeys;
}
